<!--  -->
<template>
  <div class="list-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="toAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表数据展示 -->
      <div class="goods-table">
        <el-table :data="goodsList" border stripe style="width: 100%">
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量(g)"></el-table-column>
          <el-table-column label="创建时间">
            <template slot-scope="scope">{{ scope.row | dateFormat }}</template>
          </el-table-column>

          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-row>
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  @click="toUpdateGoodsPage(scope.row.goods_id)"
                ></el-button>
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  @click="deleteGoodsById(scope.row.goods_id)"
                ></el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页信息 -->
      <div class="goods-page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [], // 商品数据列表
      total: 0, // 商品总数量
      // 查询对象
      queryInfo: {
        query: '',
        pagenum: 1, // 当前页码
        pagesize: 10 // 每页显示商品条目/数量
      }
    }
  },

  created() {
    this.getGoodsList()
  },

  methods: {
    // 获取商品列表
    getGoodsList() {
      this.axios
        .get('goods', {
          params: {
            query: this.queryInfo.query,
            pagenum: this.queryInfo.pagenum,
            pagesize: this.queryInfo.pagesize
          }
        })
        .then(resp => {
          if (resp.meta.status !== 200) {
            this.$message.error('获取商品列表失败')
          }
          this.$message.success({
            message: '获取商品列表成功',
            duration: 500
          })
          this.goodsList = resp.data.goods
          this.total = resp.data.total
        })
    },
    // 处理分页每页条目改变
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 处理分页当前页码改变
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 跳转到添加商品页面
    toAddGoodsPage() {
      this.$router.push('/goods/add')
    },
    // 跳转到修改商品页面
    toUpdateGoodsPage(goodsId) {
      this.$router.push({
        name: 'updateGoods',
        params: { goodsId: goodsId }
      })
    },
    // 删除用户
    deleteGoodsById(goodsId) {
      // 删除确认
      this.$confirm('此操作将会永久删除该商品，确定要删除吗', 'Warning', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 确认删除，执行删除操作
          this.axios.delete(`goods/${goodsId}`).then(resp => {
            if (resp.meta.status !== 200) {
              return this.$message.error('删除商品失败')
            }
            // 成功删除，重新获取数据
            this.getGoodsList()
            this.$message.success('成功删除商品')
          })
        })
        .catch(() => {
          // 取消删除
          this.$message.info('已取消删除')
        })
    }
  },

  components: {}
}
</script>

<style lang='scss' scoped>
.list-container {
  .box-card {
    .goods-table {
      margin: 10px 0;
      font-size: 12px;
    }
  }
}
</style>
